﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using Common.Entities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TomShane.Neoforce.Controls;

namespace ClickGame.General
{
    
    public class FightEntityControl :Control
    {
        private Animation _animation;
        private Texture2D _walkTexture;
        private Texture2D _fightTexture;
        private Texture2D _dieTexture;
        private Texture2D _standTexture;
        public FightEntity FightEntity;
        private int _id;
        private int _framesUntilDispose = 300;

        public int Health
        {
            get { return FightEntity.HP; }
            set
            {
                if (value <= 0)
                {
                    value = 0;
                    ChangeState(FighterState.Dying);
                }
                FightEntity.HP = value;
            }
        }

        public Vector2 Position
        {
            get { return new Vector2(FightEntity.Position.X,FightEntity.Position.Y);}
            set { FightEntity.Position = new System.Drawing.Point((int) value.X, (int) value.Y); }
        }


        public FightEntityControl(Manager manager, int type, int maxHp, Point position, Rectangle bounds, int id, int offset) : base(manager)
        {
            FightEntity = new FightEntity(type,maxHp,new System.Drawing.Point(position.X, position.Y), new System.Drawing.Rectangle(bounds.X+position.X,bounds.Y,bounds.Width,bounds.Height));
            Top = position.Y;
            Left = position.X;
            Id = id;
            //LeftModifier = -offset;
        }

        public Texture2D WalkTexture
        {
            get { return _walkTexture; }
            set { _walkTexture = value; }
        }

        public Texture2D StandTexture
        {
            get { return _standTexture; }
            set { _standTexture = value; }
        }

        public Texture2D FightTexture
        {
            get { return _fightTexture; }
            set { _fightTexture = value; }
        }

        public Texture2D DieTexture
        {
            get { return _dieTexture; }
            set { _dieTexture = value; }
        }

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public Animation Animation
        {
            get { return _animation; }
            set { _animation = value; }
        }

        public void ChangeState(FighterState state)
        {
            if(FightEntity.State == state)
                return;
            switch (state)
            {
                case FighterState.Walking:
                    Animation.ChangeTexture(WalkTexture,0.1f,true);
                    break;
                case FighterState.Fighting:
                    Animation.ChangeTexture(FightTexture, 0.1f, true);
                    break;
                case FighterState.Dying:
                    Animation.ChangeTexture(DieTexture, 0.1f, false);
                    break;
                case FighterState.Standing:
                    Animation.ChangeTexture(StandTexture, 0.1f, true);
                    break;
            }
            FightEntity.State = state;
        }

        public override void Init()
        {
            base.Init();
            Animation = new Animation(StandTexture,0.1f,true);
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            Animation.Update(gameTime);
            if (FightEntity.HP <= 0)
            {
                if (Animation.CurrentFrame == Animation.MaxFrames-1)
                {
                    _framesUntilDispose--;
                }
            }
            if(_framesUntilDispose <= 0)
                Dispose();
            Invalidate();
        }

        protected override void DrawControl(Renderer renderer, Rectangle rect, GameTime gameTime)
        {
            //base.DrawControl(renderer, rect, gameTime);
            renderer.Draw(Animation.CurrentTexture,rect,Animation.AnimationRectangle,FightEntity.PlayerOwned?Color.White:Color.Red);
        }

        public bool IntersectsWith(FightEntityControl fec)
        {
            return FightEntity.BoundingRect.IntersectsWith(fec.FightEntity.BoundingRect);
        }
    }
}
